package com.net128.beatportapps.downloader;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginDialog extends AppDialog {
	public String user;
	public String password;

	private static String[] loginOptionNames = { "Login", "Cancel" };
	private static String loginTitle = "Beatport Login";
	
	public String login() {
		String sessionCookies;
		boolean retry=false;
		do {
			tryLogin(retry);
			sessionCookies=new BeatportAccessor().getSessionCookies(user, password);
			retry=true;
		} while(sessionCookies==null);
		return sessionCookies;
	}
	
	private void tryLogin(boolean retry) {
		if(retry) {
			JLabel message=new JLabel("Failed to login into Beatport");
			message.setForeground(Color.red);
			addInput("", message, -1);
		}
		JTextField userNameField=new JTextField(20);
		JTextField passwordField = new JPasswordField(20);
		addInput("User", userNameField, -1);
		addInput("Password", passwordField, -1);
		
		if (showOptionDialog(loginTitle, JOptionPane.OK_CANCEL_OPTION,
				loginOptionNames, loginOptionNames[0], userNameField) != 0) {
			System.exit(0);
		}
		user = userNameField.getText();
		password = passwordField.getText();
	}
	
}